package com.example.emos.workflow.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author Yanx
 * @Create 2022-05-12-13:57
 */
@Slf4j
@Component
public class EmailTask implements Serializable {
    @Value("${spring.mail.username}")
    private String mailbox;

    @Autowired
    private JavaMailSender mailSender;

    @Async
    public void sendAsync(SimpleMailMessage simpleMailMessage) {
        try{
            simpleMailMessage.setFrom(this.mailbox);
            this.mailSender.send(simpleMailMessage);
            log.info("邮件发送成功"+simpleMailMessage.getTo().toString());
        }catch (Exception e){
            log.error("发送邮件失败："+ e.getMessage());
        }
    }

}